package sims;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StockRegistServlet extends HttpServlet {

	/* (非 Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		perform(req, resp);
	}

	/* (非 Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		perform(req, resp);
	}

	private void perform(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String number = req.getParameter("number");
		String strQuantity = req.getParameter("quantity");
		int quantity = 0;
		try {
			quantity = Integer.parseInt(strQuantity);
		}
		catch (NumberFormatException e) {
		}

		StockRecord rec = new StockRecord();
		rec.setNumber(number);
		rec.setQuantity(quantity);

		rec.add();

		RequestDispatcher dispatcher = req.getRequestDispatcher("StockListServlet");
		dispatcher.forward(req, resp);
	}
}
